package com.skp.shaphan.menus;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.skp.shaphan.SQLEditorsPane;
import com.skp.shaphan.ShaphanApp;
import com.skp.shaphan.layout.ConstraintSet;
import com.skp.shaphan.layout.XYConstraints;
import com.skp.shaphan.layout.XYLayout;

/**
 * Connects to a database and load SQL in the editor for testing
 * @author skp
 *
 */
public class DevLayoutTestMenuItem extends JMenuItem implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7912302846574513812L;

	public DevLayoutTestMenuItem() {
		super("Layout Information (On Stdout)");
		this.setMnemonic('L');
		this.setAccelerator(KeyStroke.getKeyStroke('L', InputEvent.CTRL_DOWN_MASK));
		this.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		SQLEditorsPane editorsPane = ShaphanApp.getMainWindow().getSQLEditorsPane();
		JPanel viewPortView = (JPanel)editorsPane.getViewport().getView();
		System.out.println("Layout = " + viewPortView.getLayout().getClass().getName());
		System.out.println("Component Count = " + viewPortView.getComponentCount());
		System.out.println("viewport = " + editorsPane.getViewport().getView().getClass().getName());
		XYLayout layout = (XYLayout)viewPortView.getLayout();
		for(Component c : viewPortView.getComponents()) {
			XYConstraints xy = layout.getConstraints(c);
			printComponent(c, xy);
		}
		System.out.println("Sorted: ");
		for(ConstraintSet c : layout.listComponents()) {
			printComponent(c.getComponent(), c.getConstraints());
		}
	}
	
	public void printComponent(Component c, XYConstraints xy) {
		System.out.println("   - " + c.getClass().getName());
		Rectangle b = c.getBounds();
		System.out.println("       x="  + b.x + " y=" + b.y + " w=" + b.width + " h=" + b.height);
		System.out.println("       x="  + xy.getX() + " y=" + xy.getY() + " w=" + xy.getWidth() + " h=" + xy.getHeight());
	}
}
